/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ucue.ptc.gco.ui;

import edu.ucue.ptc.gco.mdl.Cargo;
import edu.ucue.ptc.gco.ngc.CargoManagerLocal;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

/**
 *
 * @author Johnny
 */
@Named(value = "cargoAction")
@SessionScoped
public class CargoAction implements Serializable{

    /**
     * Creates a new instance of CargoAction
     */
    @Inject 
    private CargoManagerLocal cargoManager;
    public static CargoAction getInstance() {
        ELContext context = FacesContext.getCurrentInstance().getELContext();
        ValueExpression ex = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
                createValueExpression(context, "#{cargoAction}", CargoAction.class);
        return (CargoAction) ex.getValue(context);}
    private Cargo cargo;
    private String editAction;
    private String titulo;
   public void crearCargo()
    {
        setCargo(new Cargo());
    }
    
    public String nuevo()
    {
        setCargo(new Cargo());
        this.editAction = "crear";
        
        return "cargo";
    }
    
    public String editar(Cargo cargo){
        setCargo(cargo);
        this.editAction = "actualizar";
        
        return "cargo";
    }
    
    public String getTitulo()
    {
        if (this.editAction.equals("crear"))
                return "Crear Cargo";
            else if (this.editAction.equals("actualizar"))
                return "Actualizar Cargo";
            else if (this.editAction.equals("eliminar"))
                return "Eliminar Cargo";
        return "Cargo";
    }

    public String eliminar(Cargo cargo){
        setCargo(cargo);
        this.editAction = "eliminar";
        
        return "cargo";
    }
    
    public String grabar()
    {
        try
        {
            if (this.editAction.equals("crear"))
                cargoManager.crearCargo(getCargo());
            else if (this.editAction.equals("actualizar"))
                cargoManager.actualizarCargo(getCargo());
            else if (this.editAction.equals("eliminar"))
                cargoManager.eliminarCargo(getCargo());

            return "listaCargos";
            
        } catch (Exception e) {
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
            return null;
        }
    }
    
    public String cancelar()
    {
        return "listaCargos";
    }
    
    
    public String getEditAction() {
        return editAction;
    }

    public void setEditAction(String editAction) {
        this.editAction = editAction;
    }
    
    public CargoAction() {
    
        cargo= new Cargo();
    }

    public Cargo getCargo() {
        return cargo;
    }

    public void setCargo(Cargo cargo) {
        this.cargo = cargo;
    }
    
}
